{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45d56c67-7439-4264-912a-c0b4895cac63",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f71e64d0-f967-4244-98ba-4e5bc4530883",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "489900d6-cc33-4ada-b2be-7e3a139cf6ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "! cd modelscope-agent && pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad42f062-5664-44f8-82b7-6995a4f512a0",
   "metadata": {},
   "source": [
    "### 读取环境变量文件和工具config配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f06ad1-34c5-4032-9254-aab88d7fe19d",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from modelscope.utils.config import Config\n",
    "\n",
    "# 配置环境变量，里面需要配置modelscope token来remote调用AI模型\n",
    "os.environ['TOOL_CONFIG_FILE'] ='../config/cfg_tool_template.json'\n",
    "os.environ['MODEL_CONFIG_FILE'] ='../config/cfg_model_template.json'\n",
    "\n",
    "os.environ['OUTPUT_FILE_DIRECTORY'] = '/tmp'\n",
    "\n",
    "# ModelScope Token从这里获取: https://modelscope.cn/my/myaccesstoken\n",
    "print('请输入ModelScope Token，可以从这里获取: https://modelscope.cn/my/myaccesstoken')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()\n",
    "\n",
    "print('请输入DashScope Token，可以从这里获取: 1. 注册登录https://dashscope.aliyun.com 2. 打开模型广场选择通义千问开源系列，并申请开通通义千问7B模型 3. 预计半天通过')\n",
    "\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n",
    "\n",
    "# 读取工具调用CONFIG文件，包括工具的名称和远程请求url\n",
    "tool_cfg_file = os.getenv('TOOL_CONFIG_FILE')\n",
    "print(tool_cfg_file)\n",
    "tool_cfg = Config.from_file(tool_cfg_file)\n",
    "\n",
    "model_cfg_file = os.getenv('MODEL_CONFIG_FILE')\n",
    "print(model_cfg_file)\n",
    "model_cfg = Config.from_file(model_cfg_file)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbbee3d8-067b-405d-b630-2a74d4e78b5c",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from modelscope_agent.agent import AgentExecutor\n",
    "from modelscope_agent.llm import LLMFactory\n",
    "\n",
    "model_name = 'modelscope-agent'\n",
    "\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)\n",
    "agent = AgentExecutor(llm, tool_cfg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b53b9bb3-8380-4e6d-987c-102991f60563",
   "metadata": {},
   "source": [
    "### 执行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b4fd261-0ddf-485f-afdc-559b718b9bf3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "agent.reset()\n",
    "agent.run('写一个20字左右简短的小故事', remote=True)\n",
    "agent.run('用女声念出来', remote=False, print_info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3ce13e2-7321-4f8f-bc04-c5882730bdf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "agent.reset()\n",
    "# remote=True为调用modelscope api，该服务免费支持QPS较低，建议部署在本地，将remote=False\n",
    "agent.run(\"generate an image: a cute dog is running in the city\", remote=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
